(function(){
    "use strict";
    app.controller('userCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = ' 用户中心> 账户';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取用户列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        userName: $scope.searchByUsername,
                        roleName: $scope.searchByRole,
                        userStatus:$scope.searchByStatus
                    };
                httpLoad.loadData({
                    url:'/user/list',
                    method: 'POST',
                    dataType:"json",
                    data: params,
                    //noParam: true,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.userList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                            angular.forEach($scope.userList, function(data,index){
                                if(data.isLocked){
                                    data.isfreeze=true;
                                    data.isthaw=false;
                                }else{
                                    data.isfreeze=false;
                                    data.isthaw=true;
                                }
                            });
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //新增
            $scope.add = function(){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/add.html',
                    controller : 'addUserModalCtrl'// 初始化模态范围
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //编辑
            $scope.update = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/update.html',  //指向上面创建的视图
                    controller : 'updateUserModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    angular.extend(item,data);
                },function(){});
            };
            //详情
            $scope.detail = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/userdetail.html',  //指向上面创建的视图
                    controller : 'detailUserModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    angular.extend(item,data);
                },function(){});
            };
            //添加角色环境信息
            $scope.addRole = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/addRole.html',  //指向上面创建的视图
                    controller : 'addRoleUserModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    angular.extend(item,data);
                },function(){});
            };
            
            //删除
            $scope.remove = function(userId){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/delete.html',
                    controller : 'deleteUserModalCtrl',
                    resolve : {
                        userId: function() {
                            return  userId;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                    $scope.isCheck = false;
                },function(){});
            };
            //冻结
            $scope.freeze = function(id){
                $scope.freezeData= {"id" : id};
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/freeze.html',
                    controller : 'freezeUserModalCtrl',
                    resolve : {
                        freezeData : function(){
                            return $scope.freezeData;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //解冻
            $scope.thaw = function(id){
                $scope.thawData= {"id" : id};
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/user/thaw.html',
                    controller : 'thawUserModalCtrl',
                    resolve : {
                        thawData : function(){
                            return $scope.thawData;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
        }
    ]);

    //新增ctrl
    angular.module('app').controller('addUserModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
        function($scope,$modalInstance,LANGUAGE,httpLoad){
            $scope.ok = function(){
            	var data = $scope.addData;
            	var envId = sessionStorage.getItem('currentEnvId');
            	if(null != envId){
            		data.defaultEnvId = envId;
            	}
                httpLoad.loadData({
                    url:'/user/create',
                    method:'POST',
                    data: data,
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //编辑ctrl
    angular.module('app').controller('updateUserModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
            $scope.updateData = {};
            $scope.updateData.userId = itemData.userId;
            $scope.updateData.userName = itemData.userName;
            $scope.updateData.userMail = itemData.userMail;
            $scope.updateData.userPhone = itemData.userPhone;
            $scope.updateData.userCompany = itemData.userCompany;
            $scope.ok = function(){
                $scope.updateData.id = itemData.id;
                httpLoad.loadData({
                    url:'/user/updateUser',
                    method:'POST',
                    data: $scope.updateData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    
  //用户详情
    angular.module('app').controller('detailUserModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
    	 var userId= itemData.userId;
    	//取得当前环境
         //var envId =  sessionStorage.getItem('currentEnvId');
    	 $scope.detailData = {};
         $scope.detailData.userId = userId;
    	 httpLoad.loadData({
             url:'/user/detail',
             method:'POST',
             data: JSON.stringify({userId:userId}),
             success:function(data){
                 if(data.success){
                 	var userInfo= data.data;
                 	 $scope.detailData.userName = userInfo.USER_NAME;
                 	 $scope.detailData.ldapUid = userInfo.LDAP_UID;
                 	 $scope.detailData.userMail = userInfo.USER_MAIL;
                 	 $scope.detailData.userPhone = userInfo.USER_PHONE;
                 	 $scope.detailData.userCompany = userInfo.USER_COMPANY;
                 	 $scope.detailData.roleName = userInfo.ROLE_NAME;
                 	 $scope.detailData.envName = userInfo.ENV_NAME;
                 }else{
                	 $scope.pop(data.message);
                 }
             }
         });
            $scope.ok = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //添加角色
    angular.module('app').controller('addRoleUserModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
    	 $scope.visible = false;
    	 $scope.envshow = false;
            $scope.updateData = {};
            $scope.updateData.userId = itemData.userId;
            $scope.updateData.userName = itemData.userName;
            $scope.updateData.isAdmin = itemData.isAdmin;
            var userId= itemData.userId;
            //取得当前环境
            var envId =  sessionStorage.getItem('currentEnvId');
            httpLoad.loadData({
                url:'/role/getUnderRoles',
                method:'POST',
                data: JSON.stringify({userId:userId,envId:envId}),
                success:function(data){
                    if(data.success){
                    	var roles= data.data.roles;
                    	$scope.myroles=data.data.myrole;
                    	$scope.roles= roles;
                    }else{
                    	$scope.pop(data.message);
                    }
                }
            });
            $scope.param = {
            		rows: 5	
            	};
            $scope.changeRole = function () {
            	
            	$scope.getData = function(page){
                	  $scope.param.page = page || $scope.param.page;
                	  var roleId = $scope.param.role_id;
                      var params = {
                              page: $scope.param.page,
                              rows: $scope.param.rows,
                              roleId : roleId,
                          }
            	//var roleId = $scope.param.role_id;
            	/*if(null == roleId || 'undefind' == roleId)
            		return null;*/
            	//获取项目列表或者环境列表
            	var url = 2 == roleId?'/env/getAllEnv':'/project/getProjectByEnv'
            	 httpLoad.loadData({
                     url:url,
                     method:'POST',
                     data: JSON.stringify(params),
                     success:function(data){
                         //if(data.success){
                        	 //环境列表展示，同时取得已经选中环境的值
                           if(2==roleId){
                        	   if(data.data&&data.data.length!=0){
                        		   $scope.visible = false;
                            	   $scope.envshow = true;
                                   $scope.envList = data.data;
                                   $scope.totalCount = data.records;
                                   $scope.isImageData = false;
                                   angular.forEach($scope.envList, function(data,index){
                                       if(data.isLocked){
                                           data.isfreeze=true;
                                           data.isthaw=false;
                                       }else{
                                           data.isfreeze=false;
                                           data.isthaw=true;
                                       }
                                   });
                               }else{
                                   $scope.isImageData = true;
                               }
                           }else{
                        	   if(data.rows&&data.rows.length!=0){
                        	   $scope.envshow = false;
                        	   $scope.visible = true;
                               $scope.projectList = data.rows;
                               $scope.totalCount = data.records;
                               $scope.isImageData = false;
                               angular.forEach($scope.projectList, function(data,index){
                                   if(data.isLocked){
                                       data.isfreeze=true;
                                       data.isthaw=false;
                                   }else{
                                       data.isfreeze=false;
                                       data.isthaw=true;
                                   }
                               });
                           }else{
                               $scope.isImageData = true;
                           }
                           }
                     }
                 });
                };
                $scope.getData(1);
            };
            $scope.ok = function(){
            	var roleId = $scope.param.role_id;
            	//角色为空退出
            	if(undefined == roleId){
            		$modalInstance.dismiss('cancel'); // 退出
            		return false;
            	}
                $scope.updateData.id = itemData.userId;
            	var obj=document.getElementsByName('roleName'); //选择所有name="'test'"的对象，返回数组 
            	var userRoleEnv = new Array();//json数组
                 // 组装JSON数组
            	var jsonObj = {};
            	jsonObj["userId"]=userId;
            	jsonObj["roleId"]=roleId;
            	jsonObj["envId"]=envId;
            	userRoleEnv.push(jsonObj);
            	if(0 == userRoleEnv.length)
            			return null;
                httpLoad.loadData({
                    url:'/role/addRoleForUser',
                    method:'POST',
                    data: JSON.stringify(userRoleEnv),
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    
    //删除ctrl
    angular.module('app').controller('deleteUserModalCtrl',['$scope','$modalInstance','userId','LANGUAGE','httpLoad',
        function($scope,$modalInstance,userId,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/user/delete',
                    method:'POST',
                    data:{userId: userId},
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    //冻结ctrl
    angular.module('app').controller('freezeUserModalCtrl',['$scope','$modalInstance','httpLoad','freezeData','LANGUAGE',
        function($scope,$modalInstance,httpLoad,freezeData,LANGUAGE){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/user/lock',
                    method:'POST',
                    data: freezeData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    //解冻ctrl
    angular.module('app').controller('thawUserModalCtrl',['$scope','$modalInstance','httpLoad','thawData','LANGUAGE',
        function($scope,$modalInstance,httpLoad,thawData,LANGUAGE){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/user/active',
                    method:'POST',
                    data: thawData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
})();